package vn.com.bookstore;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public abstract class ConnectActivity extends FragmentActivity {
	public static final String INTENT_ACTION = "ConnectActivity";
	
	public Handler mHandler = new Handler();
	protected BookStoreApplication mApplication;

	public static final int METHOD_GET_BOOK = 0;
	public static final int METHOD_GET_STORE = 1;
	public static final int METHOD_GET_IMAGE = 2;
	public static final int METHOD_GET_PAGE = 3;
	public static final int METHOD_POST_FEEDBACK = 4;
	public static final int METHOD_GET_CONTENT = 5;

	BroadcastReceiver bookStoreReceiver = new BroadcastReceiver(){
		public void onReceive(Context context, android.content.Intent intent) {
			ConnectActivity.this.onReceive(context, intent);
		};
	};
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mApplication = (BookStoreApplication) this.getApplication();
		this.registerReceiver(bookStoreReceiver, new IntentFilter(INTENT_ACTION));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(bookStoreReceiver);
	}

	protected void findViewById() {
	}

	protected void refreshUI() {
	}
	
	public void onReceive(Context context, android.content.Intent intent) {
		
	}

	public void setTitle(CharSequence title, String desc) {
		super.setTitle(title);
		new HeaderLayout(this).setTitle(title + "", desc);
	}

	public void setTitle(CharSequence title, String desc, boolean notBack) {
		super.setTitle(title);
		new HeaderLayout(this, notBack).setTitle(title + "", desc);
	}

	class HeaderLayout implements OnClickListener {
		TextView title, desc;
		ImageView imv;
		ImageView back;

		public HeaderLayout(Context context, boolean notBack) {
			findViewById();
			back.setVisibility(notBack ? View.GONE : View.VISIBLE);

			if (!notBack) {
				title.setOnClickListener(this);
				desc.setOnClickListener(this);
				imv.setOnClickListener(this);
				back.setOnClickListener(this);
			}
		}

		public HeaderLayout(Context context) {
			findViewById();

			title.setOnClickListener(this);
			desc.setOnClickListener(this);
			imv.setOnClickListener(this);
			back.setOnClickListener(this);
		}

		public void findViewById() {
			title = (TextView) ConnectActivity.this.findViewById(R.id.head_title);
			desc = (TextView) ConnectActivity.this.findViewById(R.id.head_desc);
			imv = (ImageView) ConnectActivity.this.findViewById(R.id.head_image);
			back = (ImageView) ConnectActivity.this.findViewById(R.id.back);
		}

		public void setTitle(String title, String desc) {
			this.title.setText(title);
			this.desc.setText(desc);
		}

		@Override
		public void onClick(View arg0) {
			ConnectActivity.this.finish();
		}
	}
}
